import React from 'react';
import url from "../config/url";
import req from "../http/req";
import { Redirect } from 'react-router-dom';

const Checklogin = (Cmp) => {
  return class NewCmp extends React.Component {
    state = {
      isLogin: false,
      isFinish: false // 请求是否结束
    }
    render () {
      return this.state.isFinish ? this.state.isLogin ? <Cmp {...this.props}></Cmp> : <Redirect to="/login"/> : <></>
    }

    componentDidMount () {
      req.get(url.JwtCheck)
        .then(res => {
          if (res.data.errNo === 0) {
            this.setState({
              isLogin: true,
              isFinish: true
            })
          } else {
            this.setState({
              isLogin: false,
              isFinish: true
            })
          }
        })
    }
  }
}

export default Checklogin;
